package com.yiwei.hdb.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.api.bean.V3SetNewPassBean;
import com.yiwei.hdb.constant.UserGlobal;
import com.yiwei.hdb.util.MD5;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.util.UI;
import com.yiwei.hdb.util.Util;
import com.yiwei.hdb.view.Dialog;
import com.yiwei.hdb.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3SetNewPassActivity extends BaseActivity {

    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_pass)
    EditText editPass;
    @BindView(R.id.btn_see_pass)
    ImageView btnSeePass;
    @BindView(R.id.edit_pass_confirm)
    EditText editPassConfirm;
    @BindView(R.id.btn_see_pass_confirm)
    ImageView btnSeePassConfirm;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Unbinder unbinder;
    private boolean isChecked = false;
    private boolean isCheckedConfirm = false;
    private boolean isNewSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_v3_set_newpass);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        isNewSet = getIntent().getBooleanExtra("is_new_set", false);
        txtTitle.setText("设置密码");
        if (isNewSet) {
            btnBack.setVisibility(View.INVISIBLE);
            txtRight.setText("跳过");
            txtRight.setVisibility(View.VISIBLE);
        }
        setIsNeedCheckLogout(false);
    }


    @OnTextChanged({R.id.edit_pass, R.id.edit_pass_confirm})
    void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editPass)) && !TextUtils.isEmpty(UI.toString(editPassConfirm))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    public void getSetNewPass(String password, String verify_password) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        V3SetNewPassBean bean = new V3SetNewPassBean();
        bean.password = MD5.getMD5str(password);
        bean.verify_password = MD5.getMD5str(verify_password);
        Api.api_service.getSetPass(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();

                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {
                    UserGlobal.setIsSetPass();
                    ToastUtil.makeText(this, "修改成功").show();
                    if (!isSetGuesture()) {
                        Dialog.showDefaultDialog(this, "", "您尚未绘制手势密码!", "取消", "去绘制", false, new Dialog.DialogDefaultClickListener() {
                            @Override
                            public void confirm() {
                                Util.startActivity(V3SetNewPassActivity.this, CreateGestureActivity.class);
                                finish();
                            }

                            @Override
                            public void cancel() {
                                finish();
                            }
                        });
                    } else {
                        finish();
                    }

                });
    }


    private boolean checkNewPwd() {
        if (TextUtils.isEmpty(UI.toString(editPass))) {
            ToastUtil.makeText(this, getResources().getString(R.string.password_is_empty), 0);
            return false;
        }
        return true;
    }


    private boolean checkNewPwdConfirm() {
        if (TextUtils.isEmpty(UI.toString(editPassConfirm))) {
            ToastUtil.makeText(this, "新密码不能为空", 0);
            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @Override
    public void onBackPressed() {
        if (isNewSet) {

        } else {
            super.onBackPressed();
        }
    }

    @OnClick({R.id.txt_right, R.id.btn_back, R.id.btn_see_pass, R.id.btn_see_pass_confirm, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_right:
                Dialog.showDefaultDialog(this, "提示", "您可以在“我的”里面点击头像设置密码哦", "取消", "我知道了", false, new Dialog.DialogDefaultClickListener() {
                    @Override
                    public void confirm() {
                        finish();
                    }

                    @Override
                    public void cancel() {

                    }
                });

                break;
            case R.id.btn_back:
                if (!isNewSet) {
                    finish();
                }
                break;
            case R.id.btn_see_pass:
                isChecked = !isChecked;
                if (isChecked) {
                    //如果选中，显示密码
                    btnSeePass.setSelected(true);
                    editPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    btnSeePass.setSelected(false);
                    editPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.btn_see_pass_confirm:
                isCheckedConfirm = !isCheckedConfirm;
                if (isCheckedConfirm) {
                    //如果选中，显示密码
                    btnSeePassConfirm.setSelected(true);
                    editPassConfirm.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    btnSeePassConfirm.setSelected(false);
                    editPassConfirm.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.btn_submit:
                if (checkNewPwd() && checkNewPwdConfirm()) {
                    getSetNewPass(UI.toString(editPass), UI.toString(editPassConfirm));
                }
                break;
        }
    }
}
